package com.gd.deron.smartrobot.baobao.tranlate;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONObject;

import com.gd.deron.smartrobot.baobao.utils.MD5Util;

import android.util.Log;

public class BaiduTranslate {
	
	private static final String UTF8 = "utf-8";
	//申请者开发者id，实际使用时请修改成开发者自己的appid
	private static final String appId = "20161006000029732";

	//申请成功后的证书token，实际使用时请修改成开发者自己的token
	private static final String token = "eycY8TGLYg2Zb8VUFXZT";
	private static final String url = "http://api.fanyi.baidu.com/api/trans/vip/translate";
	
	public static String translate(String q,String from,String to ) throws Exception{
		
		q = URLDecoder.decode(q, UTF8);
		int salt = (int)(Math.random()*10000000);
		StringBuffer md5String = new StringBuffer();
		md5String.append(appId).append(q).append(salt).append(token);
		String md5 = MD5Util.getMd5Value(md5String.toString());
		HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
		nvps.add(new BasicNameValuePair("q", q));  
	    nvps.add(new BasicNameValuePair("from", from));  
	    nvps.add(new BasicNameValuePair("to", to));  
	    nvps.add(new BasicNameValuePair("appid", appId));  
	    nvps.add(new BasicNameValuePair("salt", String.valueOf(salt)));  
	    nvps.add(new BasicNameValuePair("sign", md5)); 
	    httpPost.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
	    HttpClient httpClient = new DefaultHttpClient();
	    HttpResponse httpResponse = httpClient.execute(httpPost);
	    HttpEntity entity = httpResponse.getEntity();
	    
	    InputStream returnStream = entity.getContent();
		BufferedReader reader = new BufferedReader(
				new InputStreamReader(returnStream, UTF8));
		StringBuilder result = new StringBuilder();
		String str = null;
		while ((str = reader.readLine()) != null) {
			result.append(str).append("\n");
		}
		
		Log.e(">>>>>>>>>>>>>>>>>", "result="+result.toString());
		JSONObject resultJson = new JSONObject(result.toString());
		//开发者自行处理错误，本示例失败返回为null
		try {
			String error_code = resultJson.getString("error_code");
			if (error_code != null) {
				System.out.println("出错代码:" + error_code);
				System.out.println("出错信息:" + resultJson.getString("error_msg"));
				return resultJson.getString("error_msg");
			}
		} catch (Exception e) {}
		//获取返回翻译结果
		JSONArray array = (JSONArray) resultJson.get("trans_result");
		JSONObject dst = (JSONObject) array.get(0);
		String text = dst.getString("dst");
		return text;
	}
}
